package com.kingeid.hlj.test.mvp.presenter;

import android.app.Application;
import android.text.TextUtils;

import com.jess.arms.base.CommonSubscriber;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import javax.inject.Inject;

import com.jess.arms.utils.SHA256Util;
import com.kingeid.hlj.test.app.utils.RxUtils;
import com.kingeid.hlj.test.mvp.contract.LoginContract;
import com.kingeid.hlj.test.mvp.model.entity.BaseResponse;
import com.kingeid.hlj.test.mvp.model.entity.User;


/**
 * ================================================
 * Description:
 * <p>
 * Created by huanglj on 11/29/2019 15:40
 * ================================================
 */
@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {

    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    public void login(String account, String pwd) {
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(pwd)) {
            mRootView.showMessage("请输入手机号和密码");
            return;
        }
        mRootView.showLoading();
        mModel.login(account, SHA256Util.getSHA256String(pwd))
                .compose(RxUtils.rxSchedulerHelper(mRootView))
                .subscribe(new CommonSubscriber<BaseResponse<String>>(mRootView) {
                    @Override
                    public void onNext(BaseResponse<String> stringBaseResponse) {
                        if (stringBaseResponse.isSuccess()) {
                            mRootView.loginSuccess(stringBaseResponse.getData());
                        } else {
                            mRootView.showMessage(stringBaseResponse.getMsg());
                        }
                    }
                });

    }

    public void getCode(String phone) {
        mRootView.showLoading();
        mModel.getCode(phone)
                .compose(RxUtils.rxSchedulerHelper(mRootView))
                .subscribe(new CommonSubscriber<BaseResponse>(mRootView) {
                    @Override
                    public void onNext(BaseResponse response) {
                        if (response.isSuccess()) {
                            mRootView.getCodeSuccess(response.getMsg());
                        } else {
                            mRootView.showMessage(response.getMsg());
                        }

                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
